package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.EmgDeviceBo;
import com.ruoyi.system.domain.vo.EmgDeviceVo;
import com.ruoyi.system.domain.EmgDevice;
import com.ruoyi.system.mapper.EmgDeviceMapper;
import com.ruoyi.system.service.IEmgDeviceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 机械设备Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@RequiredArgsConstructor
@Service
public class EmgDeviceServiceImpl implements IEmgDeviceService {

    private final EmgDeviceMapper baseMapper;

    /**
     * 查询机械设备
     */
    @Override
    public EmgDeviceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询机械设备列表
     */
    @Override
    public TableDataInfo<EmgDeviceVo> queryPageList(EmgDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmgDevice> lqw = buildQueryWrapper(bo);
        Page<EmgDeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询机械设备列表
     */
    @Override
    public List<EmgDeviceVo> queryList(EmgDeviceBo bo) {
        LambdaQueryWrapper<EmgDevice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmgDevice> buildQueryWrapper(EmgDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmgDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWarehouseId() != null, EmgDevice::getWarehouseId, bo.getWarehouseId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceTypeId()), EmgDevice::getDeviceTypeId, bo.getDeviceTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), EmgDevice::getName, bo.getName());
        lqw.eq(bo.getUserId() != null, EmgDevice::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getQuantity()), EmgDevice::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getCarNo()), EmgDevice::getCarNo, bo.getCarNo());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), EmgDevice::getModel, bo.getModel());
        lqw.eq(bo.getBuyTime() != null, EmgDevice::getBuyTime, bo.getBuyTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPower()), EmgDevice::getPower, bo.getPower());
        lqw.eq(StringUtils.isNotBlank(bo.getEndurance()), EmgDevice::getEndurance, bo.getEndurance());
        lqw.eq(StringUtils.isNotBlank(bo.getRescueLevelId()), EmgDevice::getRescueLevelId, bo.getRescueLevelId());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), EmgDevice::getLat, bo.getLat());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), EmgDevice::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), EmgDevice::getAddress, bo.getAddress());
        lqw.eq(ObjectUtil.isNotNull(bo.getDeptId()), EmgDevice::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getLeader1()), EmgDevice::getLeader1, bo.getLeader1());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone1()), EmgDevice::getPhone1, bo.getPhone1());
        lqw.eq(StringUtils.isNotBlank(bo.getLeader2()), EmgDevice::getLeader2, bo.getLeader2());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone2()), EmgDevice::getPhone2, bo.getPhone2());
        lqw.eq(StringUtils.isNotBlank(bo.getSecurityTest()), EmgDevice::getSecurityTest, bo.getSecurityTest());
        lqw.eq(StringUtils.isNotBlank(bo.getSecurityTestImage()), EmgDevice::getSecurityTestImage, bo.getSecurityTestImage());
        lqw.eq(bo.getRemindTime() != null, EmgDevice::getRemindTime, bo.getRemindTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPassFlag()), EmgDevice::getPassFlag, bo.getPassFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EmgDevice::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增机械设备
     */
    @Override
    public Boolean insertByBo(EmgDeviceBo bo) {
        EmgDevice add = BeanUtil.toBean(bo, EmgDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改机械设备
     */
    @Override
    public Boolean updateByBo(EmgDeviceBo bo) {
        EmgDevice update = BeanUtil.toBean(bo, EmgDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmgDevice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除机械设备
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
